import org.apache.spark.sql.SparkSession

object Q4_DateTransform {
  def main(args: Array[String]): Unit = {
    val spark = SparkSession
      .builder()
      .appName(this.getClass.getCanonicalName.init)
      .master("local[*]")
      .getOrCreate()
    val sc = spark.sparkContext
    sc.setLogLevel("ERROR")
    import spark.implicits._

    val rawDF =
      spark.read
        .schema("ID string, startdate string, enddate string")
        .option("sep"," ")
        .csv("F:\\TrainingCourse\\P1 大数据\\大数据正式班第四阶段模块二\\spark实战应用（上）\\代码\\data\\Q4.dat")

        val frame1 = rawDF.selectExpr("startdate as date")
        val frame2 = rawDF.selectExpr("enddate as date")
        frame1.union(frame2).sort("date").createOrReplaceTempView("t1")
    spark.sql(
      """
        |select date, LEAD(date,1,MAX(date) over()) over(order by date) date2
        |from t1
        |""".stripMargin)
      .show()
  }
}
